package view.finance
{
	import business.ApplyPayDelegate;
	
	import flash.events.MouseEvent;
	import flash.system.System;
	
	import mx.collections.ArrayCollection;
	import mx.containers.HBox;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.ComboBox;
	import mx.controls.DataGrid;
	import mx.controls.LinkButton;
	import mx.controls.RadioButtonGroup;
	import mx.controls.Text;
	import mx.controls.TextArea;
	import mx.controls.TextInput;
	import mx.rpc.events.FaultEvent;

	[Bindable]
	public class FinanceClass extends VBox
	{
		private var delegateApplyPay:ApplyPayDelegate;
		
		public var myGrid:DataGrid;
		public var m_items:ArrayCollection;
		
		public var item_box:HBox;
		
		public var currentPage:int = 1;
		
		public var item_search_mode:ComboBox;
		public var item_search_input:TextInput;
		
		public var page:int;
		public var pageIndices:Array;//页面索引
		public var pagecnt:int;
		
		public var listType:int = 0;
		public var listField:String = '';
		public var bInSearch:Boolean = false;
		
		public var searchModes:ArrayCollection = new ArrayCollection(
												[{label:"宝宝号", data:0},
												 {label:"银行账号", data:1},
												 {label:"账户姓名", data:2}
												]);

		public var searchPattern:String;
		public var searchType:String;

		public var processStatus:RadioButtonGroup;//处理状态
		public var taRemark:TextArea;//备注
		public var txAuditCashResult:Text;
		
		public function FinanceClass()
		{
			super();
        	this.delegateApplyPay = new ApplyPayDelegate(this);
		}
		
		public function onTypeClick(type:String):void
		{
			//暂时只有"录像"
		}
		
		/**
		 * 获取指定类型的转账列表
		 * @param listType 转账类型
		 */
		public function onRangeClick(listType:String):void
		{
			if(listType == 'unremite'){	//未处理
				this.listType = 0;
				this.listField = 'createTime';
			}else if(listType == 'remited'){	//已汇款
				this.listType = 1;
				this.listField = 'dealTime';
			}else if(listType == 'remitfault'){//汇款失败
				this.listType = 2;
				this.listField = 'dealTime';
			}else if(listType == 'all'){		//全部
				this.listType = 3;
				this.listField = 'createTime';
			}
			
			getCashList( this.listType, 1, 13, this.listField );
		}
		
		/**
		 * 刷新列表按钮响应函数
		 */
		public function onRefreshClick():void
		{
			this.bInSearch = false;
			this.currentPage = 1;
			getCashList( this.listType, 1, 13, this.listField );
		}
		
		/**
		 * 页面跳转
		 */
		public function goto(event:MouseEvent):void
		{
			getCashList( this.listType, int(LinkButton(event.currentTarget).data.page), 13, this.listField );
		}
		/**
		 * 页面跳转
		 */
		public function gotoPage(pageNum:String):void
		{
			if (pageNum.length > 0) {//有输入页码
				if(int(pageNum) >= 1){//页码大于等于1
					if(int(pageNum) > this.pageCount){//页码大于总页数
						this.currentPage = this.pageCount==0 ? 1 : this.pageCount;
						gotoPage(this.pageCount.toString());//跳转到最后一页
						Alert.show('已经是最后一页了');
						return;
					}
					if(!this.bInSearch)
						getCashList( this.listType, int(pageNum), 13, this.listField );
					else
						onSearch(this.item_search_mode.selectedIndex, this.item_search_input.text, this.listType, int(pageNum));
				}else{//页码小于1
					this.currentPage = 1;
					Alert.show('已经是第一页了');
				}
			}
		}
		
		/**
		 * 搜索转账记录(根据宝宝号、账户姓名、银行账号其一)
		 * @param type 根据宝宝号、账户姓名或者银行账号之一
		 * @param pattern 关键字
		 * @param listType 列表类型（未处理、已汇款、汇款失败）
		 * @param page 页码
		 * @param pageSize 每页长度
		 */
		public function onSearch(type:int, pattern:String, listType:int=0, page:int=1, pageSize:int=13):void
		{
			if (pattern.length <= 0)
				return;

			this.bInSearch = true;
			this.searchPattern = pattern;
			//this.listType = type;
			this.currentPage=1;
			
			if (type == 0)
				searchCash( this.searchPattern, '', '', listType, 1, 13 );
			else if (type == 1)
				searchCash( '', this.searchPattern, '', listType, 1, 13 );
			else if (type == 2)
				searchCash( '', '', this.searchPattern, listType, 1, 13 );
		}
		
		public function onDataGridItemClick():void{
			if(this.myGrid.selectedItem != null)
				this.item_box.enabled = true;
			else
				this.item_box.enabled =false;
		}
		
		/**
		 * 复制按钮响应函数
		 */
		public function copyText(str:String):void
		{
			if(str.length > 0){
				System.setClipboard(str);
			}else{
				System.setClipboard('');
				Alert.show('要复制的文本为空');
			}
		}
		
		/**
		 * 保存转账记录
		 */
		public function onAuditCashDone():void
		{
			auditCash( myGrid.selectedItem.id, int(processStatus.selectedValue), taRemark.text);
		}

		/**
		 * 上一行、下一行
		 */
		public function prevItem():void
		{
			if(this.myGrid.selectedIndex > 0)
				--this.myGrid.selectedIndex;
			else
				Alert.show('已经是第一条记录了');
		}
		public function nextItem():void
		{
			if(this.myGrid.selectedIndex < this.m_items.length-1)
				++this.myGrid.selectedIndex;
			else
				Alert.show('已经是最后一条记录了');
		}
		
		public function fault(error:FaultEvent,token:Object = null):void
		{
			Alert.show(error.fault.faultString,'error');
		}
		
		/**
		 * 获取财务转换列表
		 * @param status 审核状态，0=等待、1=通过、2=拒绝
		 * @param page 页数
		 * @param pageSize 每页显示条数
		 * @param orderName 排序列表(createTime=申请时间，dealTime=处理时间)
		 * @param orderDis 排序规则
		 */
		public function getCashList( status:int=0, page:int=1, pageSize:int=13, orderName:String="", orderDis:String = "DESC" ):void
		{
			this.pageSize = pageSize;
			this.delegateApplyPay.getCashList( status, page, pageSize, orderName, orderDis );
		}
		public var pageSize:int;
		public var pageCount:int;
		public function getCashListResult(result:Object, token:Object = null):void
		{
			result = result.result;

			if (result.code == 0){
				this.m_items = prepare( new ArrayCollection(result.data) );
				pageCount = (result.total/this.pageSize)==0 ? 1 : (result.total%this.pageSize)>0 ? (result.total/this.pageSize)+1 : result.total/this.pageSize;
			}else{
				Alert.show(result.msg);
			}
		}
		
		public var date:Date = new Date();
		private function prepare( data:ArrayCollection ):ArrayCollection
		{
			for each (var obj:Object in data){
				this.date.setTime(obj.createTime * 1000 );
				obj._applyTime = date.getFullYear() + '年' + (date.getMonth()+1) + '月' + date.getDate() + '日 ' + date.getHours() + ':' + date.getMinutes();
			}
			return data;
		}
		
		/**
		 * 查询财务转换记录(bid,account,accountName三选一)
		 * @param bid 用户id
		 * @param account 用户银行帐号
		 * @param accountName 用户银行帐号名
		 * @param status 审核状态
		 * @param page 页数
		 * @param pageSize 每页显示条数
		 */
		public function searchCash( bid:String, account:String, accountName:String, status:int=0, page:int=1, pageSize:int=13 ):void
		{
			this.delegateApplyPay.searchCash(bid, account, accountName, status, page, pageSize);
		}
		public function searchCashResult(result:Object, token:Object = null):void
		{
			result = result.result;
Alert.show(new ArrayCollection(result.data).length.toString(), 'result总数');
			if (result.code == 0){
				this.m_items = new ArrayCollection(result.data);
			}else{
				Alert.show(result.msg);
			}
		}
		
		/**
		 * 审核财务记录
		 * @param id 记录id号
		 * @param status 设置处理状态
		 * @param notes 备注
		 */
		public function auditCash( id:int, status:int, notes:String=""):void
		{
			this.delegateApplyPay.auditCash(id, status, notes );
		}
		public function auditCashResult(result:Object, token:Object = null):void
		{
			result = result.result;
			
			if (result.code == 0){
				this.processStatus.selectedValue = 0;
				this.taRemark.text = '';
				this.txAuditCashResult.text = '保存成功！';
				//Alert.show("保存成功！");
			}else{
				Alert.show(result.msg);
			}
		}
	}
}